﻿using System;

namespace parser
{
	/// <summary>
	/// Description of Parser.
	/// </summary>
	public class Parser
	{
		/// <summary>
		/// Parse expression from lexem list
		/// Expecting that: expr = expr [operator expr]
		/// </summary>
		/// <param name="lxs"></param>
		private static TreeNode Parse(Lexem[] lxs, int beg, int end)
		{
			var n = end-beg+1;
			if(n==1)
			{
				if (lxs[beg].lt == LexemType.VALUE)
				{
					return new TreeNode(lxs[beg]);
				}
				else
				{
					throw new ExceptionError("Unexpected Type");
				}
			}
			else
			{
				
				var val = lxs[beg];
				var op = lxs[beg+1];
				var tr = new TreeNode(op);
				tr.left = new TreeNode(val);
				tr.right = Parse(lxs, beg+2,end);
				return tr;
			}
		}
		
		public static TreeNode Parse(Lexem[] lxs)
		{
			return Parse(lxs, 0, lxs.Length - 1);
		}
		
		
		public Parser()
		{
		}
	}
}
